<template>
  <el-form ref="ruleForm" class="setLogin" :model="ruleForm" status-icon :rules="rules" label-width="100px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="ruleForm.username" />
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">更新</el-button>
      <el-button @click="$router.back()">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { setUser } from '@/api/employees'
export default {
  props: ['userInfo'],
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码的长度在6-16位之间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.ruleForm.username = this.userInfo.username
  },
  methods: {
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate(async(valid) => {
        if (valid) {
          try {
            await setUser({ ...this.userInfo, ...this.ruleForm })
            this.$message({
              type: 'success',
              message: '更新成功'
            })
          } catch (error) {
            this.$message({
              type: 'error',
              message: '更新失败'
            })
          }
        }
      })
    }
  }
}
</script>

<style>

</style>
